package com.wang.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.entity.UserEntity;
import com.wang.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Wang
 * @Data: 2022/3/24 16:47
 * @Description:
 */
@Service
public class UserService {
    private final static Logger log = LoggerFactory.getLogger(UserService.class);

    @Resource
    UserMapper userMapper;

    /**
     * @return
     * 获取数据总量
     */
    public int getCount() {
        log.info("获取数据总量");
        int result = userMapper.selectCount(null);
        return result;
    }

    /**
     * @return
     * 查询所有用户
     */
    public List<UserEntity> getAllUsers() {
        log.info("查询所有用户");
        return userMapper.selectList(null);
    }

    /**
     * @param id
     * @return
     * 根据id查询用户
     */
    public UserEntity getUserById(Integer id) {
        log.info("根据id查询用户");
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return userMapper.selectOne(wrapper);
    }

    /**
     * @return
     * 增加一个用户
     */
    public void addUser(UserEntity userEntity) {
        log.info("增加一个用户");
        userMapper.insert(userEntity);
    }

    /**
     * @param id
     * @return
     * 根据id删除一个用户
     */
    public void deleteUserById(Integer id) {
        log.info("根据id删除一个用户");
        userMapper.deleteUserById(id);
    }

    /**
     * @param userEntity
     * @return
     * 根据id修改用户
     */
    public void updateUserById(UserEntity userEntity) {
        log.info("根据id修改用户");
        userMapper.updateUserById(userEntity);
    }

    /**
     * @return
     * 获取最大主键
     */
    public Integer maxId() {
        log.info("获取最大主键id+1，方便插入");
        return userMapper.maxId() + 1;
    }
}
